package DeadLockDemo;

import javax.crypto.spec.SecretKeySpec;
import java.security.*;
import java.util.ArrayList;
import java.util.Arrays;

public class CryPto {
    public static final KEM KEM =null ;

    // 假设KEM是一个加密类
    static class KEM {
        public static KEM getInstance(String algorithm) {
            return new KEM();
        }

        public Encapsulator newEncapsulator(PublicKey publicKey) {
            return new Encapsulator();
        }

        public Decapsulator newDecapsulator(PrivateKey privateKey, Encapsulated enc) {
            return new Decapsulator();
        }

        public static class Encapsulator {
            public Encapsulated encapsulate() {
                return new Encapsulated();
            }
        }

        public static class Decapsulator {
            public byte[] decapsulate() {
                return new byte[32]; // 示例返回
            }
        }

        public static class Encapsulated {
            public Key key() {
                return new SecretKeySpec(new byte[32], "KYBER"); // 示例返回
            }
        }
    }


    public static void main(String[] args) throws NoSuchAlgorithmException {
        // 1. 密钥对生成
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("KYBER");
        KeyPair kp = kpg.generateKeyPair();

        // 2.密钥封装
        KEM kem1 = KEM.getInstance("KYBER");
        KEM.Encapsulator sender = kem1.newEncapsulator(kp.getPublic());
        KEM.Encapsulated enc = sender.encapsulate();
        byte[] secretkey = enc.key().getEncoded();

        // 3.密钥解封
        KEM kem2 = KEM.getInstance("KYBER");
        KEM.Decapsulator receiver = kem2.newDecapsulator(kp.getPrivate(), enc);
        byte[] secretkey2 = receiver.decapsulate();

        System.out.println(Arrays.equals(secretkey, secretkey2));
        System.out.println(Arrays.toString(secretkey2));
        System.out.println(Arrays.toString(secretkey));

        ArrayList<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        System.out.println("列表内容: " + list);
    }
}
